require(['config'], function () {
    require(['jquery'], function () {
        $('#footer').load('footer.html .footer');
        require(['jquery-cookie'], function () {
            function init() {
                //取元素
                this.$carttable = $('.cart-list .cart-table');
                this.$allprice = $('.fs14 span');
                this.$delall = $('.delall');
                this.$delchk = $('.delchk');
                //初始化变量
                let _this = this;
                let idarr = []; //存放sid 
                let numarr = []; //存放数量
                let indexarr = [];
                //判断是否有cookie
                if ($.cookie('cookieid') && $.cookie('cookienum')) {
                    //取cookie
                    idarr = $.cookie('cookieid').split(',');
                    numarr = $.cookie('cookienum').split(',');

                    //ajax取数据 渲染
                    const phpurl = 'http://10.31.155.44/02/yiguo/php/';
                    // const phpurl = ' http://10.31.155.44/02/git/fkc/yiguo/php/';

                    //渲染
                    this.render = function () {
                        $.ajax({
                            url: phpurl + 'goodlist.php',
                            dataType: 'json'
                        }).done(function (data) {
                            data = data[1];
                            let strhtml = '';
                            for (let i = 0; i < idarr.length; i++) {
                                for (let value of data) {
                                    if (value.id === idarr[i]) {
                                        strhtml += `
                                        <tr>
                                        <td class="cart-t-check"><input type="checkbox" checked="checked"></td>
                                        <td class="cart-t-img"><a href="details.html?${value.id}"><img
                                                    src="${value.url}"></a>
                                        </td>
                                        <td class="cart-t-info"><a
                                                href="details.html?${value.id}">${value.title}</a>
                                            <p class="red"> </p>
                                        </td>
                                        <td class="cart-t-ub" style="width:75px;"></td>
                                        <td class="cart-t-price">${value.price}</td>
                                        <td class="cart-t-num">
                                            <div class="quantity-form">
                                                <a href="javascript:;" class="decrement"></a>
                                                <input index="${i}" shopid="${value.id}" type="text" class="itxt" value="${numarr[i]}">
                                                <a href="javascript:;" class="increment"></a>
                                            </div>
                                        </td>
                                        <td class="cart-t-total">￥<span>${Math.round(100 * value.price) * numarr[i] / 100}</span></td>
                                        <td class="cart-t-spec">1盒/份</td>
                                        <td class="cart-t-opera">
                                            <a href="javascript:;">移入收藏</a>
                                            <br>
                                            <a class="cartdelet" href="javascript:;">删除</a>
                                        </td>
                                    </tr>
                                        `;
                                        break;
                                    }
                                }
                            }
                            _this.$carttable.children('tbody').html(strhtml);
                            _this.check();
                            _this.itxtchange();
                            _this.count();
                        });
                    };

                    //执行
                    this.render();

                } else {
                    //如果购物车为空
                    this.empty = function () {

                    }
                    //执行
                    this.empty();
                }



                //取渲染元素
                this.element = function () {
                    this.$total = $('.cart-t-total span');
                    this.$decrement = $('.decrement');
                    this.$increment = $('.increment');
                    this.$input = $('.itxt');
                    this.$price = $('.cart-list .cart-t-price');
                    this.$del = $('.cartdelet');
                    this.$tr = $('.cart-list tr');
                    this.$checkbox = $(".cart-list input[type='checkbox']");
                }
                //改变数量及删除功能
                this.change = function () {
                    //方法

                    //改变商品数量的事件绑定
                    for (let i = 0; i < this.$decrement.length; i++) {
                        this.$decrement.eq(i).on('click', function () {
                            _this.$input.eq(i).val(Number(_this.$input.eq(i).val()) - 1);
                            _this.disable(i);
                            let id = _this.$input.eq(i).attr('shopid');
                            _this.addcout(id, i);
                            _this.price(i);
                        });

                        this.$increment.eq(i).on('click', function () {
                            _this.$input.eq(i).val(Number(_this.$input.eq(i).val()) + 1);
                            let id = _this.$input.eq(i).attr('shopid');
                            _this.addcout(id, i);
                            _this.price(i);
                        });

                        this.$input.eq(i).on('input', function () {
                            let id = _this.$input.eq(i).attr('shopid');
                            _this.disable(i);
                            _this.addcout(id, i);
                            _this.price(i);
                        });

                        this.$del.eq(i).on('click', function () {
                            if (window.confirm('你确定要删除该商品吗')) {
                                let id = _this.$input.eq(i).attr('shopid');
                                let index = _this.$input.eq(i).attr('index');
                                indexarr.push(index);
                                _this.removecookie(id, i);
                                _this.$tr.eq(i).remove();
                                _this.count();
                            }
                        });
                    }
                }
                //购物车页面改变商品数量
                this.itxtchange = function () {
                    this.element();
                    this.change();
                };
                //计算总价
                this.count = function () {
                    let count = 0;
                    let flag = true;
                    for (let i = 0; i < this.$total.length; i++) {
                        flag = true;
                        if (this.$checkbox.eq(i).prop('checked')) {
                            for (let j = 0; j < indexarr.length; j++) {
                                if (i == indexarr[j]) {
                                    flag = false;
                                    break;
                                }
                            }
                            // console.log(flag);
                            // console.log(indexarr);
                            if (flag) {
                                count += Math.round(this.$total.eq(i).html() * 100);
                            }
                        };
                    }
                    this.$allprice.html(count / 100);
                }

                //当商品数量等于1时关闭减少点击
                this.disable = function (i) {
                    if (this.$input.eq(i).val() > 1) {
                        this.$decrement.removeAttr('disabled');
                    } else {
                        this.$decrement.attr({ 'disabled': 'disabled' });
                        this.$input.val(1);
                    }
                }
                //商品数量改变时修改价格
                this.price = function (i) {
                    this.$total.eq(i).html(Math.round(100 * this.$price.eq(i).html()) * this.$input.eq(i).val() / 100);
                    this.count();
                }
                //添加到数据库
                this.addcout = function (id, i) {
                    for (let j = 0; j < idarr.length; j++) {
                        if (idarr[j] == id) {
                            numarr[j] = parseInt(this.$input.eq(i).val());
                            $.cookie('cookienum', numarr.toString(), { expires: 7 });
                        }
                    }
                }
                //从数据库中删除
                this.removecookie = function (id) {
                    for (let j = 0; j < idarr.length; j++) {
                        if (idarr[j] == id) {
                            idarr.splice(j, 1);
                            numarr.splice(j, 1);
                            $.cookie('cookienum', numarr.toString(), { expires: 7 });
                            $.cookie('cookieid', idarr.toString(), { expires: 7 });
                        }
                    }
                }
                //全选/选择框
                this.check = function () {
                    this.$checkall = $(".chkAll");
                    this.$checkbox = $(".cart-list input[type='checkbox']");
                    for (let i = 0; i < this.$checkbox.length; i++) {
                        this.$checkbox.eq(i).on('click', function () {
                            _this.$checkboxnew = $(".cart-list input[type='checkbox']");
                            let flagnum = 0;
                            for (let i = 0; i < _this.$checkboxnew.length; i++) {
                                if (_this.$checkboxnew.eq(i).prop("checked")) {
                                    flagnum++;
                                }
                            }
                            if (flagnum < _this.$checkboxnew.length) {
                                _this.$checkall.removeAttr('checked');

                            } else if (flagnum == _this.$checkboxnew.length) {
                                _this.$checkall.prop("checked", true);
                            }
                            _this.count();
                        });
                    }
                    this.$checkall.on('click', function () {
                        if ($(this).prop('checked')) {
                            _this.$checkbox.prop("checked", true);
                            _this.$checkall.prop("checked", true);
                        } else {
                            _this.$checkbox.removeAttr('checked');
                            _this.$checkall.removeAttr('checked');
                        }
                        _this.count();
                    });
                }
                //清空购物车
                this.$delall.on('click', function () {
                    if (Number(_this.$carttable.children('tbody').html()) !== 0) {
                        if (window.confirm('请确认是否清空')) {
                            $.cookie('cookienum', null, { expires: -1 });
                            $.cookie('cookieid', null, { expires: -1 });
                            for (let i = 0; i < _this.$checkbox.length; i++) {
                                let sid = _this.$input.eq(i).attr('shopid');
                                _this.removecookie(sid);
                                _this.$tr.eq(i).remove();
                                let index = _this.$input.eq(i).attr('index');
                                indexarr.push(index);
                            }
                            _this.count();
                        }
                    }
                });

                //删除选中的商品
                this.$delchk.on('click', function () {
                    if (Number(_this.$carttable.children('tbody').html()) !== 0) {
                        if (window.confirm('确定删除选中项?')) {
                            for (let i = 0; i < _this.$checkbox.length; i++) {
                                let cbox = _this.$checkbox.eq(i);
                                if (cbox.prop('checked')) {
                                    let sid = _this.$input.eq(i).attr('shopid');
                                    _this.removecookie(sid);
                                    _this.$tr.eq(i).remove();
                                    let index = _this.$input.eq(i).attr('index');
                                    indexarr.push(index);
                                }
                            }
                            _this.count();
                        }
                    }
                });


            }
            init();
        });
    });
});